package com.lib.common.helper.sharehelper.share

import android.content.Context
import android.os.Bundle
import com.lib.common.helper.sharehelper.AShareListener
import com.lib.common.helper.sharehelper.entity.ShareData
import com.lib.common.helper.sharehelper.entity.ShareType
import com.lib.framework.base.BaseActivity
import com.lib.framework.manager.thread.mainThreadRun
import com.tencent.connect.share.QzoneShare
import com.tencent.tauth.IUiListener
import com.tencent.tauth.UiError
import com.xiangwushuo.thirdparty.qq.QQHelper

/**
 * Copyright (C), 2019-2020
 * FileName: QQShare
 * Author: Kevin
 * Date: 2020/3/11 17:03
 * Description: qq 空间 分享
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class QQZoneShare(val context: Context) : IShare {

    override fun init() {
        QQHelper.init(context.applicationContext)
    }

    override fun getShareType() = ShareType.QQ_ZONE_SHARE

    override fun share(activity: BaseActivity, shareData: ShareData, l: AShareListener): Boolean {
        val params = Bundle()
        params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT)
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareData.title)
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareData.description)
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareData.url)

        val imageUrl = shareData.imgUri?.toString()
        if (!imageUrl.isNullOrEmpty()) {
            val imgUrlList = ArrayList<String>()
            imgUrlList.add(imageUrl)
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imgUrlList)
        }

        QQHelper.shareToQzone(activity, params, object : IUiListener {
            override fun onComplete(p0: Any?) {
                mainThreadRun {
                    l.onShareSuccess()
                }
            }

            override fun onCancel() {
                mainThreadRun {
                    l.onShareCancel()
                }
            }

            override fun onError(p0: UiError?) {
                mainThreadRun {
                    l.onShareFail("分享失败")
                }
            }
        })
        return true
    }
}